<script setup lang="ts">
import Login from "../components/Login.vue";
import {stuLogin} from "../apis/LoginApi.ts";
import router from "../router";
import {ElMessage} from "element-plus";
import {onMounted} from "vue";
import { dayjs } from 'element-plus'
import {useStore} from "../store";

const store = useStore();

const handler = async (accout:string, password:string) => {
  let studentRes = await stuLogin(accout, password);
  if (studentRes.flag){
    ElMessage({
      message: studentRes.msg,
      type: "success",
    })
      //将用户信息存储到pinia中
      store.studentIsLogin = true;
      //记录最近登录时间
      let time = dayjs().format('YYYY-MM-DD HH:mm:ss')
      store.studentInfo = studentRes.data;
      store.studentInfo.loginTime = time;

    await router.push("/student/index");
  }else{
    ElMessage({
      message: studentRes.msg,
      type: "warning",
    })
  }
};

onMounted(async () => {

})
</script>

<template>
    <Login source="学生" @handler="handler"/>
</template>

<style scoped>

</style>